package com.greenantmall.activity.fragment;

import java.util.ArrayList;
import java.util.List;

import android.app.Fragment;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.avos.avoscloud.AVUser;
import com.greenantmall.R;
import com.greenantmall.adapter.ExpressDaiQuAdapter;
import com.greenantmall.adapter.ExpressYiQuAdapter;
import com.greenantmall.bean.EMRecv;
import com.greenantmall.service.RetrieveDataAsyn;
import com.greenantmall.service.RetrieveDataListener;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnPullEventListener;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;
import com.handmark.pulltorefresh.library.PullToRefreshBase.State;
import com.handmark.pulltorefresh.library.PullToRefreshListView;

public class TabFragmentExpress extends Fragment {
	private Button mTabBtnLeft;
	private Button mTabBtnRight;
	private ManagerDaiQu mManagerDaiQu;
	private ManagerYiQu mManagerYiQu;
	private List<EMRecv> mEMRecvList = new ArrayList<EMRecv>();
	private boolean isLoadingData = false;

	private OnClickListener mOnClickListenerFotTabBtn = new OnClickListener() {
		@Override
		public void onClick(View v) {
			if (v.equals(mTabBtnLeft)) {
				mTabBtnLeft.setSelected(true);
				mTabBtnRight.setSelected(false);
				mTabBtnLeft
						.setTextColor(getResources().getColor(R.color.white));
				mTabBtnRight.setTextColor(getResources()
						.getColor(R.color.green));
				mManagerDaiQu.display(true);
				mManagerYiQu.display(false);
			} else if (v.equals(mTabBtnRight)) {
				mTabBtnLeft.setSelected(false);
				mTabBtnRight.setSelected(true);
				mTabBtnLeft
						.setTextColor(getResources().getColor(R.color.green));
				mTabBtnRight.setTextColor(getResources()
						.getColor(R.color.white));
				mManagerDaiQu.display(false);
				mManagerYiQu.display(true);
			}
		}
	};

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Log.d("CEXX", "CEXX => TabFragmentExpress => onCreate");
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		Log.d("CEXX", "CEXX => TabFragmentExpress => onCreateView");
		return inflater
				.inflate(R.layout.tab_fragment_express, container, false);
	}

	@Override
	public void onViewCreated(View view, Bundle savedInstanceState) {
		super.onViewCreated(view, savedInstanceState);
		Log.d("CEXX", "CEXX => TabFragmentExpress => onViewCreated");

		mTabBtnLeft = (Button) view
				.findViewById(R.id.fragment_express_tab_btn_daiqu);
		mTabBtnRight = (Button) view
				.findViewById(R.id.fragment_express_tab_btn_yiqu);

		mTabBtnLeft.setOnClickListener(mOnClickListenerFotTabBtn);
		mTabBtnRight.setOnClickListener(mOnClickListenerFotTabBtn);
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		Log.d("CEXX", "CEXX => TabFragmentExpress => onActivityCreated");

		// 初始显示“待取件”
		mTabBtnLeft.setSelected(true);
		mTabBtnRight.setSelected(false);
		mTabBtnLeft.setTextColor(getResources().getColor(R.color.white));
		mTabBtnRight.setTextColor(getResources().getColor(R.color.green));
		mManagerDaiQu = new ManagerDaiQu();
		mManagerYiQu = new ManagerYiQu();
		mManagerDaiQu.display(true);
		mManagerYiQu.display(false);
		// 加载数据
		refreshData(0);
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		Log.d("CEXX", "CEXX => TabFragmentExpress => onDestroy");
	}
	
	/**
	 * 切换账户时调用
	 */
	public void reloadData() {
		isLoadingData = false;
		mManagerDaiQu.dataLoaded(new ArrayList<EMRecv>());
		mManagerYiQu.dataLoaded(new ArrayList<EMRecv>());
		this.refreshData(0);
	}
	
	/**
	 *  flag(0：全部；1：待取；2：已取)
	 */
	private void refreshData(int flag) {
		Log.d("CEXX", "CEXX > TabFragmentExpress > refreshData > flag : " + flag);
		if(AVUser.getCurrentUser() == null) {
			return;
		}
		
		if(isLoadingData) {
			return;
		}
		isLoadingData = true;
		if(flag == 1) {
			mManagerDaiQu.dataLoading();
		} else if (flag == 2) {
			mManagerYiQu.dataLoading();
		} else {
			mManagerDaiQu.dataLoading();
			mManagerYiQu.dataLoading();
		}
		RetrieveDataAsyn.getInstance().getEMRecvList(-1, new RetrieveDataListener() {
			String userId = AVUser.getCurrentUser().getObjectId();
			@Override
			public void dataRetrieved(int requestId, Object data) {
				if(AVUser.getCurrentUser() == null 
						|| !AVUser.getCurrentUser().getObjectId().equals(userId)) {
					return;
				}
				
				isLoadingData = false;
				mEMRecvList.clear();
				mEMRecvList.addAll((List<EMRecv>)data);
				
				List<EMRecv> daiquList = new ArrayList<EMRecv>();
				List<EMRecv> yiquList = new ArrayList<EMRecv>();
				for(EMRecv emRecv : mEMRecvList) {
					// status:0-无意义 1-待取，2-已取
					if(emRecv.getStatus() == 1) {
						daiquList.add(emRecv);
					} else if (emRecv.getStatus() == 2) {
						yiquList.add(emRecv);
					}
				}
				mManagerDaiQu.dataLoaded(daiquList);
				mManagerYiQu.dataLoaded(yiquList);
			}
		});
	}
	
	public void qujianSuccessful() {
		List<EMRecv> daiquList = new ArrayList<EMRecv>();
		List<EMRecv> yiquList = new ArrayList<EMRecv>();
		for(EMRecv emRecv : mEMRecvList) {
			// status:0-无意义 1-待取，2-已取
			if(emRecv.getStatus() == 1) {
				daiquList.add(emRecv);
			} else if (emRecv.getStatus() == 2) {
				yiquList.add(emRecv);
			}
		}
		mManagerDaiQu.dataLoaded(daiquList);
		mManagerYiQu.dataLoaded(yiquList);
	}

	private class ManagerDaiQu implements OnRefreshListener<ListView>,
			OnPullEventListener<ListView> {
		View daiquLayout;
		PullToRefreshListView pullToRefreshListView;
		ProgressBar progressBar;
		TextView tvPrompt;

		List<EMRecv> dataList = new ArrayList<EMRecv>();
		ExpressDaiQuAdapter adapter;

		public ManagerDaiQu() {
			daiquLayout = getView().findViewById(
					R.id.fragment_express_layout_daiqu);
			progressBar = (ProgressBar) getView().findViewById(
					R.id.fragment_express_prgressbar_daiqu);
			tvPrompt = (TextView) getView().findViewById(
					R.id.fragment_express_tv_prompt_daiqu);
			pullToRefreshListView = (PullToRefreshListView) getView()
					.findViewById(R.id.fragment_express_prlv_daiqu);

			pullToRefreshListView.setOnRefreshListener(this);
			pullToRefreshListView.setOnPullEventListener(this);
			progressBar.setVisibility(View.GONE);
			tvPrompt.setVisibility(View.VISIBLE);

			adapter = new ExpressDaiQuAdapter(TabFragmentExpress.this, dataList);
			pullToRefreshListView.setAdapter(adapter);
		}

		public void display(boolean dis) {
			if (dis) {
				daiquLayout.setVisibility(View.VISIBLE);
			} else {
				daiquLayout.setVisibility(View.INVISIBLE);
			}
		}

		@Override
		public void onRefresh(PullToRefreshBase<ListView> refreshView) {
			this.refreshDaiquData();
		}

		@Override
		public void onPullEvent(PullToRefreshBase<ListView> refreshView,
				State state, Mode direction) {
			Log.d("CEXX",
					"CEXX => TabFragmentExpress => onPullEvent => state : "
							+ state);
			if (State.PULL_TO_REFRESH.equals(state)) {
				// mSlidingMenuManage.setCanScroll(false);
			} else if (State.RESET.equals(state)) {
				// mSlidingMenuManage.setCanScroll(true);
			}
		}

		private void refreshDaiquData() {
			if (isLoadingData) {
				dataLoading();
				return;
			}
			refreshData(1);
		}
		
		public void dataLoading() {
			Log.d("CEXX", "CEXX > TabFragmentExpress > ManagerDaiQu > dataLoading");
			progressBar.setVisibility(View.VISIBLE);
			tvPrompt.setVisibility(View.GONE);
		}
		
		public void dataLoaded(List<EMRecv> emRecvList) {
			Log.d("CEXX", "CEXX > TabFragmentExpress > ManagerDaiQu > dataLoaded");
			dataList.clear();
			dataList.addAll(emRecvList);
			adapter.notifyDataSetChanged();
			pullToRefreshListView.onRefreshComplete();
			progressBar.setVisibility(View.GONE);
			if(dataList.size() == 0) {
				tvPrompt.setVisibility(View.VISIBLE);
			} else {
				tvPrompt.setVisibility(View.GONE);
			}
		}
	}

	private class ManagerYiQu implements OnRefreshListener<ListView>,
			OnPullEventListener<ListView> {
		View yiquLayout;
		PullToRefreshListView pullToRefreshListView;
		ProgressBar progressBar;
		TextView tvPrompt;
		
		List<EMRecv> dataList = new ArrayList<EMRecv>();
		ExpressYiQuAdapter adapter;

		public ManagerYiQu() {
			yiquLayout = getView().findViewById(
					R.id.fragment_express_layout_yiqu);
			progressBar = (ProgressBar) getView().findViewById(
					R.id.fragment_express_prgressbar_yiqu);
			tvPrompt = (TextView) getView().findViewById(
					R.id.fragment_express_tv_prompt_yiqu);
			pullToRefreshListView = (PullToRefreshListView) getView()
					.findViewById(R.id.fragment_express_prlv_yiqu);

			pullToRefreshListView.setOnRefreshListener(this);
			pullToRefreshListView.setOnPullEventListener(this);
			progressBar.setVisibility(View.GONE);
			tvPrompt.setVisibility(View.VISIBLE);

			adapter = new ExpressYiQuAdapter(getActivity(), dataList);
			pullToRefreshListView.setAdapter(adapter);
		}

		public void display(boolean dis) {
			if (dis) {
				yiquLayout.setVisibility(View.VISIBLE);
			} else {
				yiquLayout.setVisibility(View.INVISIBLE);
			}
		}

		@Override
		public void onRefresh(PullToRefreshBase<ListView> refreshView) {
			this.refreshYiquData();
		}

		@Override
		public void onPullEvent(PullToRefreshBase<ListView> refreshView,
				State state, Mode direction) {
			Log.d("CEXX",
					"CEXX => TabFragmentExpress => onPullEvent => state : "
							+ state);
			if (State.PULL_TO_REFRESH.equals(state)) {
				// mSlidingMenuManage.setCanScroll(false);
			} else if (State.RESET.equals(state)) {
				// mSlidingMenuManage.setCanScroll(true);
			}
		}

		private void refreshYiquData() {
			if (isLoadingData) {
				dataLoading();
				return;
			}
			refreshData(2);
		}
		
		public void dataLoading() {
			Log.d("CEXX", "CEXX > TabFragmentExpress > ManagerYiQu > dataLoading");
			progressBar.setVisibility(View.VISIBLE);
			tvPrompt.setVisibility(View.GONE);
		}
		
		public void dataLoaded(List<EMRecv> emRecvList) {
			Log.d("CEXX", "CEXX > TabFragmentExpress > ManagerYiQu > dataLoaded");
			dataList.clear();
			dataList.addAll(emRecvList);
			adapter.notifyDataSetChanged();
			pullToRefreshListView.onRefreshComplete();
			progressBar.setVisibility(View.GONE);
			if(dataList.size() == 0) {
				tvPrompt.setVisibility(View.VISIBLE);
			} else {
				tvPrompt.setVisibility(View.GONE);
			}
		}
	}
}
